<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Session;

use DB;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $value = Session::get('uname');
        //实例化操作表
        $list = DB::table('order')->where('uname',$value)->get();
        // dd($list);
        //执行分页查询
        // $list = $ob->paginate(5);
        //定义两个数组来存储商品的标题和图片
        foreach ($list as  $v) {
            // var_dump($v);
            $v->g = DB::table('detail')->where('id',$v->pid)->get();
        }

        // dd($list);
        //将不同状态的订单显示出来
        $list2 = DB::table('order')->where([
            ['uname', '=', $value],
            ['state', '=', '1'],
        ])->get();
        foreach ($list2 as  $v) {
            // var_dump($v);
            $v->g = DB::table('detail')->where('id',$v->pid)->get();
        }
        // $goods = DB::table('goods')->where('id',$ob->pid);
        // dd($goods);
        // dd($noPay);
        // $list2 = $noPay->paginate(5);

        $list3 = DB::table('order')->where([
            ['uname', '=', $value],
            ['state', '=', '2'],
        ])->get();
        // dd($noPay);
        // $list3 = $noShipp->paginate(5);
         foreach ($list3 as  $v) {
            // var_dump($v);
            $v->g = DB::table('detail')->where('id',$v->pid)->get();
        }

        $list4 = DB::table('order')->where([
            ['uname', '=', $value],
            ['state', '=', '3'],
        ])->get();
        // $list4 = $noReceive->paginate(5);
        foreach ($list4 as  $v) {
            // var_dump($v);
            $v->g = DB::table('detail')->where('id',$v->pid)->get();
        }


        $list5 = DB::table('order')->where([
            ['uname', '=', $value],
            ['state', '=', '4'],
        ])->get();
        foreach ($list5 as  $v) {
            // var_dump($v);
            $v->g = DB::table('detail')->where('id',$v->pid)->get();
        }


        // $list5 = $noSuggest->paginate(5);

        return view('home.order',['list'=>$list,'list2'=>$list2,'list3'=>$list3,'list4'=>$list4,'list5'=>$list5 ]);
         // return view('home.order');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // dd($id);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($id);
         $row = DB::table('order')->where('id',$id)->update(['state'=>4]);

         return redirect('home/order');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $row = DB::table('order')->where('id',$id)->delete();
        if($row>0){
            return redirect('home/order')->with('msg','删除成功');
        }else{
            return redirect('home/order')->with('msg','删除失败');
        }
    }

    public function dopay($id)
    {  
        $row = DB::table('order')->where('id',$id)->update(['state'=>3]);
        // return 111;
        return redirect('home/order');
    }
}
